﻿using Book.Data;
using Book.Entities;
using Book.IRepositories;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace Book.Repositiories
{
    public class LabMemberRepository:ILabMemberRepository
    {
        private readonly BookDbContext _DbContext;

        public LabMemberRepository(BookDbContext bookDbContext)
        {
            this._DbContext = bookDbContext;
        }
       /// <summary>
       /// 
       /// </summary>
       /// <param name="name"></param>
       /// <param name="yjfx">研究方向</param>
       /// <param name="identity"></param>
       /// <param name="photo"></param>
       /// <param name="photoname"></param>
       /// <param name="title"></param>
       /// <param name="order"></param>
       /// <param name="phone"></param>
       /// <param name="address"></param>
       /// <param name="email"></param>
       /// <returns></returns>
        public string AddMember(string name, string yjfx, string identity, string photo, string photoname,
            string title,double order,string phone,string address,string email)
        {
            ////获取当前的时间，一当作图片的名字
            //string fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond.ToString();
            ////获取图片的扩展名
            //string Extent = System.IO.Path.GetExtension（photo.PostedFile.FileName);
            ////重命名图片
            //fileName += Extent;
            ////设置上传图片保存的文件夹
            //string dir = System.Web.HttpContext.Current.Server.MapPath(UpladURL);
            ////指定图片的路径及文件名
            //string path = dir + "\\" + fileName;
            ////把上传得图片保存到指定的文件加中
            //FUSShopURL.PostedFile.SaveAs(path);
            //return fileName;
            //var path= Base64StringToImage(photo);
            string fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + photoname;
            string path = "/img/" + fileName;
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\img", fileName);           
            Base64ToImg(photo.Split(',')[1]).Save(filePath);           
            LabMemberEntity labmember = new LabMemberEntity
            {
                Id = Guid.NewGuid().ToString("N").Substring(0, 29),
                Name = name,
                Yjfx = yjfx,
                Identity = identity,
                Photo= path,
                Title=title,
                Order=order,
                Phone=phone,
                Address=address,
                Email=email,
                Introduce="请输入...",
                Kycg= "请输入...",
                Kyxm= "请输入...",
                Achievement= "请输入...",
                IntroduceMD = "请输入...",
                KycgMD = "请输入...",
                KyxmMD = "请输入...",
                AchievementMD = "请输入..."
            };
            _DbContext.member.Add(labmember);
            _DbContext.SaveChanges();
            return labmember.Id;
        }
       
        /// <summary>
        /// 获得所有成员
        /// </summary>
        /// <returns></returns>
        public List<LabMemberEntity> GetAllMember()
        {
            //List<LabMemberEntity> memberentity = _DbContext.member.ToList();
            List<LabMemberEntity> memberList = _DbContext.member.ToList();
            foreach (LabMemberEntity member in memberList)
            {
                member.Photo = "http://121.4.176.83:90" + member.Photo;
                //member.Photo = "http://localhost:5000" + member.Photo;
            }
            return memberList;
        }
        /// <summary>
        /// 根据身份查找成员
        /// </summary>
        /// <param name="identity"></param>
        /// <returns></returns>
        public List<LabMemberEntity> GetMemberByIdentity(string identity)
        {
            List<LabMemberEntity> memberList = _DbContext.member.Where(x => x.Identity == identity).OrderBy(x => x.Order).ToList();
            foreach (LabMemberEntity member in memberList)
            {
                member.Photo = "http://121.4.176.83:90" + member.Photo;
                //member.Photo = "http://localhost:5000" + member.Photo;
            }
            return memberList;
        }
        /// <summary>
        /// 根据id查找成员
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public LabMemberEntity GetMemberById(string id)
        {
            LabMemberEntity member = _DbContext.member.Find(id);
            member.Photo = "http://121.4.176.83:90" + member.Photo;
            //member.Photo = "http://localhost:5000" + member.Photo;
            return member;
        }
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="yjfx"></param>
        /// <param name="identity"></param>
        /// <param name="photo"></param>
        /// <param name="title"></param>
        /// <param name="order"></param>
        /// <param name="phone"></param>
        /// <param name="address"></param>
        /// <param name="email"></param>
        public void UpdateMember(string id,string name, string yjfx, string identity, string photo, string photoname,
                       string title, double order, string phone, string address, string email)
        {
            string fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + photoname;
            string path = "/img/" + fileName;
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\img", fileName);
            Base64ToImg(photo.Split(',')[1]).Save(filePath);
            LabMemberEntity labMemberEntity = _DbContext.member.Find(id);
            labMemberEntity.Name = name;
            labMemberEntity.Yjfx = yjfx;
            labMemberEntity.Identity = identity;
            labMemberEntity.Photo = path;
            labMemberEntity.Title = title;
            labMemberEntity.Order = order;
            labMemberEntity.Phone = phone;
            labMemberEntity.Address = address;
            labMemberEntity.Email = email;
            _DbContext.member.Update(labMemberEntity);
            _DbContext.SaveChanges();
        }
        /// <summary>
        /// 更新个人主页信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="value"></param>
        /// <param name="content"></param>
        /// <param name="contentmd"></param>
        public void UpdatePPage(string id,  string value, string content,string contentmd)
        {
            if (content==""|| contentmd == "")
            {
                content = "请输入...";
                contentmd = "请输入...";
            }
            LabMemberEntity labMemberEntity = _DbContext.member.Find(id);
            switch (value)
            {
                case "Introduce":labMemberEntity.Introduce = content;
                                 labMemberEntity.IntroduceMD = contentmd;
                                 break;
                case "Kyxm":labMemberEntity.Kyxm = content;
                            labMemberEntity.KyxmMD = contentmd; 
                            break;
                case "Kycg": labMemberEntity.Kycg = content;
                             labMemberEntity.KycgMD = contentmd; 
                             break;
                case "Achievement": labMemberEntity.Achievement = content;
                                    labMemberEntity.AchievementMD = contentmd; 
                                    break;
                default:break;
            }
            _DbContext.member.Update(labMemberEntity);
            _DbContext.SaveChanges();
        }
        /// <summary>
        /// 删除成员
        /// </summary>
        /// <param name="id"></param>
        public void DeleteMember(string id)
        {
            LabMemberEntity labMemberEntity = _DbContext.member.Find(id);
            _DbContext.member.Remove(labMemberEntity);
            _DbContext.SaveChanges();
        }
        //base64编码的文本 转为    图片
        //private string Base64StringToImage(string txtFileName)
        //{

        //        FileStream ifs = new FileStream(txtFileName, FileMode.Open, FileAccess.Read);
        //        StreamReader sr = new StreamReader(ifs);

        //        String inputStr = sr.ReadToEnd();
        //        byte[] arr = Convert.FromBase64String(inputStr);
        //        MemoryStream ms = new MemoryStream(arr);
        //        Bitmap bmp = new Bitmap(ms);
        //        string path = @"D:\301\图片\11.jpeg";
        //        bmp.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);
        //        //bmp.Save(txtFileName + ".bmp", ImageFormat.Bmp);
        //        //bmp.Save(txtFileName + ".gif", ImageFormat.Gif);
        //        //bmp.Save(txtFileName + ".png", ImageFormat.Png);
        //        ms.Close();
        //        sr.Close();
        //        ifs.Close();
        //        return path;



        //}
        private Bitmap Base64ToImg(string base64Code)
        {
            MemoryStream stream = new MemoryStream(Convert.FromBase64String(base64Code));
            return new Bitmap(stream);
        }

    }
}
